<template>
  <div class="login_container">
    <div class="login_container__left flex-1">
      <img src="@/assets/login/左侧bg.png" />
      <img src="@/assets/login/logo.png" alt="logo">
      <span>这里是文字实例,最多显示两行折行</span>
    </div>
    <main class="flex-1">
      <div class="content  flex-jucalic animate__animated animate__bounceInRight">
        <span class="login__title">欢迎登录</span>
        <label for="email">
          <span>Email</span>
          <el-input v-model="login.email"></el-input>
        </label>
        <label for="password">
          <span>密码</span>
          <div>
            <el-input :type="showPassword ? 'text' : 'password'" v-model="login.password"
              @focus="removepassword"></el-input>
            <span class="ispassword iconfont" @click="showPassword = !showPassword"
              :class="showPassword ? 'icon-mimaxianshi' : 'icon-icon_794'"></span>
          </div>
        </label>
        <div class="footer flex">
          <el-checkbox label="记住我" size="large" :checked="login.rememberPassword" @change="setrememberPassword" />
          <span class="forget">忘记密码?</span>
        </div>
        <el-button type="primary" @click="sendLogin">立即登录</el-button>
        <div class="send_register">
          <span>没有账号?</span>
          <span class="register" @click="sendRegister">去注册</span>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ILogin } from "@/types/register"
import { loginStore } from '@/pinia/loginStore'
import { userStore } from '@/pinia/userStore';
import { LoginApi } from '@/api/userApi';

const router = useRouter()
const route = useRoute()
const showPassword = ref(false)
const loginstore = loginStore()
const userstore = userStore()

const login = reactive<ILogin>({
  email: loginstore.rawEmail(),
  password: loginstore.rawPassword(),
  rememberPassword: loginstore.rememberPassword(),
})


//  发送登录请求
function sendLogin() {
  LoginApi(login.email, login.password).then(res => {
    // 判断是否勾选记住密码
    if (login.rememberPassword) {
      loginstore.setLocalLogin(login)
    }
    // 是否登录成功
    if (res.code == 200) {
      const { user, token, accessToken } = res.data;
      // 设置用户
      userstore.setAll(user, "token", token, true)
      // 设置刷新token
      userstore.setToken("refreshToken", accessToken)
      router.replace((route.query.toPage as string) ?? "/")

    }

  }).catch(err => err)


}
/**
 * 设置密码
 */
function setrememberPassword() {
  login.rememberPassword = !login.rememberPassword;
  loginstore.setRememberPassword(login.rememberPassword)
}

// 清除密码
function removepassword() {
  login.password = ""
  loginstore.removePassword()
}

// 跳转到注册页面
function sendRegister() {

  router.push("/reigster")
}
</script>

<style lang="scss" scoped>
.login_container {
  overflow: hidden;
  margin-top: 20px;
  width: 100%;
  height: 944px;
  background-color: white;
  border-radius: 30px;
  display: flex;

  &__left {
    position: relative;

    &>img {
      width: 100%;
      height: 100%;

      &:nth-of-type(2) {
        width: 50px;
        height: 50px;
        border-radius: 10px;
        position: absolute;
        top: 70px;
        left: 80px;
      }
    }

    &>span {
      font-size: 40px;
      position: absolute;
      top: 160px;
      left: 80px;
      width: 370px;
      text-align: left;
      font-family: "PingFang";
      color: white;
    }
  }

  &>main {

    .content {
      margin: -170px auto 0;
      width: 350px;
      height: 100%;
      flex-direction: column;
      gap: 20px;

      >.login__title {
        font-size: 26px;
      }

      >label {

        width: 350px;
        text-align: left;

        >span {
          color: #7D8592;
          display: inline-block;
          margin: 6px 0;
        }

        >div {
          position: relative;
        }
      }

      >.footer {
        width: 100%;
        justify-content: space-between;
        align-items: center;

        &>.forget {
          font-size: 14px;
          color: #7D8592;
        }
      }

      >.send_register {
        color: #3F8CFF;
        font-size: 16px;
      }

      .register {
        text-decoration: underline;
        margin-left: 6px;
      }

      .ispassword {
        position: absolute;
        top: 50%;
        right: 5px;
        font-size: 24px;
        width: 30px;
        height: 30px;
        line-height: 30px;
        transform: translateY(-50%);
      }
    }

  }
}
</style>